﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Data;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {
        public void YGRVKKDEK()
        {
            string msg = "";
            string sdkey = "";
            try
            {
                if (!dsSource.Tables.Contains("VAART"))
                {
                    return;
                }
                if (!dsSource.Tables.Contains("VAFARBEN"))
                {
                    return;
                }
                string src = "";
                string sgb = "";
                DataTable tbVKKOPF = dsSource.Tables["VKKOPF"];
                DataTable tbVKPOS = dsSource.Tables["VKPOS"];
                DataRow dwVKPOS1 = tbVKPOS.Rows[0];
                string KRS;
                string LFD;
                string POS;
                string TXTISIND;
                string str1 = "", str2 = "";
                DataRow dwVKK = tbVKKOPF.Rows[0];
                DataTable tbVKPBEST = new DataTable(), tbVKK07 = new DataTable();
                DataTable TTGAAUF = new DataTable();
                string VKPMEEH = "";
                DataRow dwVKP = tbVKPOS.Rows[0];

                string order = "";
                DataTable tbVA07 = dsSource.Tables["VAART"];
                DataTable tbVA10 = dsSource.Tables["VAFARBEN"];
                DataRow dwVA07 = null; 
                double VKKQTY = 0;
                string currency = dwVKK["VKKHWAEHRSL"].ToString();
                VKPMEEH = dwVKP["VKPMEEH"].ToString();
                string VKPPREH = dwVKP["VKPPREH"].ToString();
                #region=============Header==================
               double USD2RMB=Convert.ToDouble(dwVKK["VKKKURS"].ToString())*0.0001;
                //src = "确认号码/Proforma Invoice：" + dwVKK["VKKAUFKRS"].ToString() + "-" + System.Convert.ToInt64(dwVKK["VKKAUFLFD"].ToString());
               order = dwVKK["VKKAUFKRS"].ToString() + "-" + Right(dwVKK["VKKAUFLFD"].ToString(), 4);
               exAgent.SetCellValue(3, 32, order, true);
                exAgent.SetCellValue(4, 32, date);
                exAgent.RenameSheet(order);
                //src = dwVKK["VKKBESTNR"].ToString();
                //exAgent.SetCellValue(4, 8, src);
                //src = dwVKK["VKKBESTDAT"].ToString();
                //exAgent.SetCellValue(5, 8, src);
                DataRow dwHR = null;
                var qpr = from DataRow row in dsSource.Tables["HRSTAMM"].AsEnumerable()
                          where row["HRBENID"].ToString().Trim() == dwVKK["VKKSACHBE"].ToString().Trim()
                          select row;
                foreach (DataRow row in qpr)
                {
                    dwHR = row;
                    exAgent.SetCellValue(8, 28, GetGB2312Value(dwHR["HRNNAME"].ToString().Substring(0, 6)));
                    exAgent.SetCellValue(9, 28,dwHR["HRPTEL"].ToString().Trim());
                    exAgent.SetCellValue(10, 28, dwHR["HRDFAX"].ToString().Trim());
                }
                #region=====Buyer======


                DataRow dwKD03 = null;
                var qkd = from dwTemp in dsSource.Tables["KDSTAMM"].AsEnumerable()
                          where dwTemp["KDKUNDNR"].ToString() == dwVKK["VKKKUNDNR"].ToString()
                          select dwTemp;
                foreach (DataRow row in qkd)
                {
                    dwKD03 = row;
                }
                if (dwKD03 != null)
                {
                    //   src = "客户编号/Client No:";
                    //exAgent.SetCellValue(8, 30, src); 
                    string kdnr = dwKD03["KDKUNDNR"].ToString();
                    //src = dwKD03["KDKNAME"].ToString();
                    //exAgent.SetCellValue(4, 7, Helper.GetGB2312Value(src).Trim());
                    string kdname = dwKD03["KDNAME1"].ToString();
                    exAgent.SetCellValue(6, 5, Helper.GetGB2312Value(kdname).Trim() + "(编号:" + kdnr + ")");
                    exAgent.SetCellValue(7, 5, Helper.GetGB2312Value(dwKD03["KDSTRASSE1"].ToString()).Trim());
                    exAgent.SetCellValue(8, 5, Helper.GetGB2312Value(dwVKK["VKKANSPRECH"].ToString()).Trim());
                   exAgent.SetCellValue(9, 5, dwKD03["KDTEL"].ToString().Trim());
                    exAgent.SetCellValue(10, 5, dwKD03["KDFAX"].ToString().Trim());
                    /* src = dwKD03["KDMAILADR"].ToString();
                   exAgent.SetCellValue(10, 1, Helper.GetGB2312Value(src).Trim()); */

                }
                #endregion
                #endregion

                src = dwVKK["VKKZAHLBED"].ToString();//付款方式
                var qz = from dwTemp in dsSource.Tables["ICC_TXTKON"].AsEnumerable()
                         where dwTemp["TXTIPIND"].ToString().Trim() == src.Trim() + "V"
                         select dwTemp;
                foreach (DataRow row in qz)
                {
                    exAgent.SetCellValue(130, 5, row["TXT010"].ToString());
                }

                src = dwVKK["VKKVERSART"].ToString();//运输方式Truck  
                exAgent.SetCellValue(131, 5, Helper.GetSDDAT(dsSource, "0208", src, "V").Trim()); 
                //交易类型FOB
                src = dwVKK["VKKLIEFBED"].ToString();//交易类型FOB 
                Helper.GetSDDAT(dsSource,"0241", src,"V").Trim(); 

                #region============其他要求ANGE===================
                /*
                tbVKK07 = new DataTable();
                KRS = dwVKK["VKKAUFKRS"].ToString();
                LFD = dwVKK["VKKAUFLFD"].ToString();
                TXTISIND = KRS + LFD + "000" + "ANGE";
                var qvor = from dwTemp in dsSource.Tables["TXTKON"].AsEnumerable()
                           where dwTemp["TXTIPIND"].ToString().Contains(TXTISIND)
                           select dwTemp;
                if (qvor.Count() > 0)
                {
                    tbVKK07 = qvor.CopyToDataTable();
                }

                text = "";
                foreach (DataRow dwTXTKON in tbVKK07.Rows)
                {
                    for (int ti = 1; ti < 40; ti++)
                    {
                        string ci = "TXT" + Right("00" + ti, 2) + "0";
                        src = dwTXTKON[ci].ToString(); sgb = GetGB2312Value(src).Trim();
                        if (sgb.Trim() == "")
                            continue;
                        exAgent.SetCellValue(20 - 1 + ti, 5, sgb);
                    }
                }
                */
                #endregion


                int xStart = 19;
                int rown = 2;
                int idx = 0;
                string finish = "";
                double TAmount = 0;
                int count = tbVKPOS.Rows.Count;
                foreach (DataRow dwVKPOS in tbVKPOS.Rows)
                {
                    string color = "";
                    dwVA07 = null;
                    EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
                    POS = dwVKPOS["VKPAUFPOS"].ToString();
                    exAgent.SetCellValue(xStart + idx * rown, 1, Helper.GetSubRollNum(POS));                  
                    exAgent.SetCellValue(xStart + idx * rown, 2, dwVKPOS["VKPART"].ToString().Trim());
                    exAgent.SetCellValue(xStart + idx * rown, 5, dwVKPOS["VKPFARBE"].ToString().Trim());
        
                    #region================成份===================
                    var qVA07 = from DataRow row in tbVA07.AsEnumerable()
                                where row["VAAART"].ToString() == dwVKPOS["VKPART"].ToString()
                                select row;
                    foreach (DataRow row in qVA07)
                    {
                        dwVA07 = row;
                    }
                    if (dwVA07 != null)
                    {
                        src = Helpers.Helper.GetCompositionVA(dsSource, dwVA07,"V");
                        exAgent.SetCellValue(xStart + idx * rown, 23, GetGB2312Value(src));
                    }
                    #endregion
                    #region================客人花号/品名======================
                    src = dwVKPOS["VKPKDARTNR"].ToString().TrimEnd() + " " + dwVKPOS["VKPKDBEZ"].ToString().TrimEnd() + " " + dwVKPOS["VKPKDFARBNR"].ToString().TrimEnd();
                    var qvakd = from DataRow row in dsSource.Tables["VAKDBEZ"].AsEnumerable()
                                where row["VAKBKOLL"].ToString() == dwVKPOS["VKPKOLL"].ToString()
                                && row["VAKBART"].ToString() == dwVKPOS["VKPART"].ToString()
                                && row["VAKBFARBE"].ToString() == dwVKPOS["VKPFARBE"].ToString()
                                select row;
                    foreach (DataRow rkd in qvakd)
                    {
                        src = rkd["VAKBKDARTNR"].ToString().TrimEnd() + " " + rkd["VAKBKDARTBEZ"].ToString().TrimEnd() + " " + rkd["VAKBKDFARBNR"].ToString().TrimEnd();
                    }
                    //exAgent.SetCellValue(xStart + idx * rown + 1, 2, GetGB2312Value(src));
                    #endregion
 
                    #region================数量===================
                    exAgent.SetCellValue(17, 18, "11" + Environment.NewLine + "(" + VKPMEEH + ")");
                    src = dwVKPOS["VKPMENGE1"].ToString();
                    string VKPMENGE1 = Divide(src, 100);
                    VKKQTY += System.Convert.ToDouble(VKPMENGE1);
                    exAgent.SetCellValue(xStart + idx * rown, 11, VKPMENGE1);

                    exAgent.SetCellValue(17, 15, "单价" + Environment.NewLine + " (" + currency + "/" + VKPPREH + ")");
                    src = dwVKPOS["VKPPREIS"].ToString();
                    exAgent.SetCellValue(xStart + idx * rown, 15, Divide(src, 1000));
                    
                    exAgent.SetCellValue(17, 19, "金额(" + currency + ")");
                    double pvalue = System.Math.Round(Convert.ToDouble(dwVKPOS["VKPPOSWERT"].ToString()) / USD2RMB, 2);
                    TAmount += System.Convert.ToDouble(pvalue);
                    exAgent.SetCellValue(xStart + idx * rown, 19, pvalue);

               
                    #endregion
                    #region =================交期======================
                    exAgent.SetCellValue(xStart + idx * rown, 32, dwVKPOS["VKPWUTERM"].ToString());
                    //exAgent.SetCellValue(130, 13,"确认:"+ dwVKPOS["VKPLIEFTERM"].ToString(), true);
                    #endregion
                    #region =================标准====================

                    string std = GetGB2312Value(dwVKPOS["COLORSTD"].ToString().Trim());
                    /*foreach (DataRow dwTXTKON in tbSTD.Rows)
                    {
                        for (int ti = 1; ti < 40; ti++)
                        {
                            string ci = "TXT" + Right("00" + ti, 2) + "0";
                            src = dwTXTKON[ci].ToString(); sgb = GetGB2312Value(src).Trim();
                            text = sgb.TrimEnd();
                            if (text.Trim() == "")
                                break;
                            int idxd = text.IndexOf(".");
                            if (idxd > 0)
                            {
                                string line = text.Substring(0, idxd);
                                line = Right("000" + line, 3);
                                if (line == POS)
                                    std += text.Substring(idxd + 1);
                            }
                        }
                    }*/
                    exAgent.SetCellValue(xStart + idx * rown, 27, std);
                    #endregion
                    idx++;
                    if (idx < count)
                    {
                        exAgent.RangeCopy(19, 20, xStart + idx * rown, xStart + idx * rown + 1);
                    }

                }
                exAgent.SetCellValue(117, 11, VKKQTY, true);
                exAgent.SetCellValue(117, 19, TAmount, true);
       
            }
            catch (Exception ex)
            {
                msg = "有错误产生，请注意:";
                EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                msg = "指令表虽然已经生产，但请注意核对。";
                EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
            }
        }
    
    }
}
